//----------------------------------------------------------
// Engine.cpp
//   Core engine / game class definition
// Loosely based off code from DeVry University, GSP420
//----------------------------------------------------------

#include <windows.h>
#include "Engine.h"
#include "Utilities/Variables.h"

#include "Graphics_GameDX.h"

// constructor
Engine::Engine()
{
	// initialize to overwrite junk values
	_fps = 0.0f;
	_frameTime = 0.0f;
	_isPaused = false;
	_isOver = false;
}

// destructor
Engine::~Engine()
{
	// do nothing
	// test update
}

bool Engine::Initialize(HWND *wndHandle, HINSTANCE *hInst)
{
	_wndHandle = wndHandle;
	_hInst = hInst;

	// create core managers
	_graphicsManager = new GraphicsManager();
	_hudManager = new HUDManager();

	// initialize managers
	if (!_graphicsManager->Initialize(new Graphics_GameDX(), this))	// create DirectX graphics core
	{
		MessageBox(NULL, "Unable to initialize GraphicsManager", "ERROR", MB_OK);
		return false;
	}

	if (!_hudManager->Initialize(((Graphics_GameDX*)_graphicsManager->GetGraphics())->GetD3DDevice(), this, GAME_STRING))
	{
		MessageBox(NULL, "Unable to initialize HUDManager", "ERROR", MB_OK);
		return false;
	}

	return true;
}

bool Engine::Update(float deltaTime)
{
	// calculate the fps based on the time between updates
	_fps = deltaTime > 0 ? 1000.0f / deltaTime : 0;

	// store deltaTime as frameTime for manager use
	_frameTime = deltaTime;

	// core managers update here
	if (!_graphicsManager->Update(deltaTime))
	{
		MessageBox(NULL, "Unable to update GraphicsManager", "ERROR", MB_OK);
		return false;
	}

	// pause implementation
	if (!_isPaused)
	{
		// do stuff
	}
	else
	{
		// pause menu
	}

	return true;
}

bool Engine::Render()
{
	// render everything on screen

	// initiate the DirectX scene
	if (!_graphicsManager->RenderStart())
	{
		MessageBox(NULL, "Unable to start GraphicsManager rendering", "ERROR", MB_OK);
		return false;
	}	

	if (!_hudManager->Draw())
	{
		MessageBox(NULL, "Unable to draw HUD", "ERROR", MB_OK);
		return false;
	}

	// render and end the DirectX scene
	_graphicsManager->RenderEnd();

	return true;
}

bool Engine::ShutDown()
{
	// shut down cores here

	if (!_graphicsManager->Shutdown())
	{
		MessageBox(NULL, "Unable to shutdown GraphicsManager", "ERROR", MB_OK);
		delete _graphicsManager;
		return false;
	}
	else 
		delete _graphicsManager;

	if (!_hudManager->Shutdown())
	{
		MessageBox(NULL, "Unable to shutdown HUDManager", "ERROR", MB_OK);
		delete _hudManager;
		return false;
	}
	else 
		delete _hudManager;

	return true;
}